package com.stu.serive.impl;

import com.stu.Dto.ExcuseApplicationDTO;
import com.stu.constant.SchoolConstant;
import com.stu.constant.StatusConstant;
import com.stu.context.BaseContext;
import com.stu.mapper.ExcuseApplicationMapper;
import com.stu.pojo.ExcuseApplication;
import com.stu.serive.ExcuseApplicationService;
import com.stu.serive.aTService;
import com.stu.vo.ExcuseApplicationVo;
import com.stu.vo.StayApplicationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class ExcuseApplicationImpl implements ExcuseApplicationService {

    @Autowired
    private ExcuseApplicationMapper excuseApplicationMapper;

    /**
     * 添加请假申请信息
     * @param excuseApplicationDTO
     */
    public void insertExcuseApplication(ExcuseApplicationDTO excuseApplicationDTO) {
        //获取学生id
        Long currentId = BaseContext.getCurrentId();
        //创建实体对象
        ExcuseApplication excuseApplication = new ExcuseApplication();
        //赋值
        BeanUtils.copyProperties(excuseApplicationDTO, excuseApplication);
        excuseApplication.setStuId(Math.toIntExact(currentId));
        //操作数据库
        excuseApplicationMapper.insertExcuseApplication(excuseApplication);

    }

    /**
     * 查询请假申请信息
     * @return
     */
    public List<ExcuseApplicationVo> selectStayApplication() {
        //获取学生id
        Long currentId = BaseContext.getCurrentId();
        //根据学生id查询 留校申请表和学生基础信息表 获取Vo数据
        List<ExcuseApplicationVo> excuseApplicationVoList =
                excuseApplicationMapper.selectStayApplicationWithStu(currentId);
        log.info("excuseApplicationVoList:{}",excuseApplicationVoList);
        //对审核状态进行判断
        for (ExcuseApplicationVo excuseApplicationVo : excuseApplicationVoList) {
            if (Objects.equals(excuseApplicationVo.getStatus(), "1")){
                excuseApplicationVo.setStatus(StatusConstant.STATUS_1);
            } else if (Objects.equals(excuseApplicationVo.getStatus(), "0")) {
                excuseApplicationVo.setStatus(StatusConstant.STATUS_0);
            }else {
                excuseApplicationVo.setStatus(StatusConstant.STATUS_2);
            }
            //添加学年和学期信息
            if( LocalDate.now().getMonth().getValue() < Month.AUGUST.getValue()){
                excuseApplicationVo.setSemester(SchoolConstant.SEMESTER_2);
            }else {
                excuseApplicationVo.setSemester(SchoolConstant.SEMESTER_1);
            }
            excuseApplicationVo.setStudyYear(SchoolConstant.STUDYYEAR);
        }
        return excuseApplicationVoList;
    }

    /**
     * 查询所有请假申请
     * @return
     */
    public List<ExcuseApplicationVo> selectStayApplicationAll() {
        return excuseApplicationMapper.selectStayApplicationAll();
    }
}
